﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xplang.run;
using xplang.abstractions;
using System.Diagnostics;

namespace xplang.statements
{
  [DebuggerDisplay("if({expression}{trueStatement}{falseStatement}")]
  public class If : Statement
  {

    public If(Expression expression, Expression trueStatement, Expression falseStatement)
    {
      this.expression = expression;
      this.trueStatement = trueStatement;
      this.falseStatement = falseStatement;
    }

    public readonly Expression expression;
    public readonly Expression trueStatement;
    public readonly Expression falseStatement;

    public override Value doIt(Value[] heap, ref FlowState state)
    {
      bool v = (bool)(expression.doIt(heap, ref state).pointer);
      if (state == FlowState.Normal)
        if (v)
          trueStatement.doIt(heap, ref state);
        else if (falseStatement != null)
          falseStatement.doIt(heap, ref state);
      return null;
    }

    public override bool isBlock()
    {
      return true;
    }
    //public override void save(Scope scope, SourceBuilder target)
    //{
    //    target.write("return");
    //    if (expression != null)
    //    {
    //        target.write(" ");
    //        expression.save(scope,target);
    //    }
    //    target.writeln(";");
    //}

    //public override void toCSharp(Scope scope, SourceBuilder target)
    //{
    //    target.write("return");
    //    if (expression != null)
    //    {
    //        target.write(" ");
    //        expression.toCSharp(scope, target);
    //    }
    //    target.writeln(";");
    //}

    //public override void toJavaScript(Scope scope, SourceBuilder target)
    //{
    //    target.write("return");
    //    if (expression != null)
    //    {
    //        target.write(" ");
    //        expression.toCSharp(scope, target);
    //    }
    //    target.writeln(";");
    //}
  }
}
